package com.cz.background.task;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cz.background.common.util.MailHelper;
import com.cz.background.dto.ey.DoctorSchedule;
import com.cz.background.dto.ey.ApiQueryScheduleDTO;
import com.cz.background.entity.ey.EySchedule;
import com.cz.background.mapper.ey.EyScheduleMapper;
import com.cz.background.service.ey.EyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class EyTask {

    private final static Logger log = LoggerFactory.getLogger(EyTask.class);


    TimedCache<String, String> cache = CacheUtil.newTimedCache(1000 * 60 * 30);

    @Autowired
    private EyScheduleMapper eyScheduleMapper;

    @Autowired
    private EyService eyService;

    @Scheduled(fixedRate = 60000)
    public void querySourceAndInfo() {
        log.info("开始查询预约信息");
        List<EySchedule> scheduleList = eyScheduleMapper.findByStatus(1);
        if (CollUtil.isNotEmpty(scheduleList)) {
            for (EySchedule schedule : scheduleList) {
                String dockerCode = schedule.getDoctorCode();
                String scheduleDate = schedule.getScheduleDate();
                String cacheKey = dockerCode + scheduleDate;
                String cacheValue = cache.get(dockerCode + scheduleDate);
                log.info("查询缓存" + cacheKey + "值为" + cacheValue);
                if (StrUtil.isBlank(cacheValue)) {
                    log.info("开始查询医生" + schedule.getDoctorName() + "的预约信息");
                    ApiQueryScheduleDTO apiQueryScheduleDTO = new ApiQueryScheduleDTO();
                    apiQueryScheduleDTO.setDrCode(dockerCode);
                    apiQueryScheduleDTO.setScheduleBegDate(schedule.getScheduleDate());
                    apiQueryScheduleDTO.setScheduleEndDate(schedule.getScheduleDate());
                    List<DoctorSchedule> doctorScheduleList = eyService.getSourceInfo(apiQueryScheduleDTO);
                    if (CollUtil.isNotEmpty(doctorScheduleList)) {
                        for (DoctorSchedule doctorSchedule : doctorScheduleList) {
                            List<DoctorSchedule.Schedule> innerScheduleList = doctorSchedule.getDrScheduList();
                            if (CollUtil.isNotEmpty(innerScheduleList)) {
                                innerScheduleList.forEach(scheduleInner -> {
                                    List<DoctorSchedule.TimeInterval> timeIntervalList = scheduleInner.getTimeintervallist();
                                    if (CollUtil.isNotEmpty(timeIntervalList)) {
                                        for (DoctorSchedule.TimeInterval timeInterval : timeIntervalList) {
                                            String beginTime = timeInterval.getBegntime();
                                            String endTime = timeInterval.getEndtime();
                                            String leftNum = timeInterval.getLeftNum();
                                            log.info("医生" + schedule.getDoctorName() + "在" + scheduleDate + "的" + beginTime + "到" + endTime + "有空余" + leftNum + "张");
                                            if (Integer.parseInt(leftNum) > 0) {
                                                MailHelper.sendMail("673741115@qq.com", "预约提醒", "您预约的医生" + schedule.getDoctorName() + "在" + scheduleDate + "的" + beginTime + "到" + endTime + "有空余" + leftNum + "张，请及时登录预约系统查看。");
                                                cache.put(dockerCode + scheduleDate, "1");
                                            }

                                        }
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }
    }
}
